//
//  ThemeManager.swift
//  Exchange
//
//  Created by rui on 2018/4/19.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

enum ThemeMode: Int {
    case day = 0
    case night = 1
}


struct Theme: Hashable {
    var hashValue: Int {
        get {
            guard let state = themeState else {
                return themeKey.hashValue
            }
            return "\(state)\(themeKey)".hashValue
        }
    }
    var themeKey: String
    var themeState: UInt?
}

class ThemeManager {
    static let `default` = ThemeManager()
    
    private var currentTheme = [String:Any]()
    
    private var themeMode = ThemeMode(rawValue: AppCacheInfo.default.theme)!
    
    private init() {
        loadThemeInfo()
    }
    private func loadThemeInfo() {
        var path = "day.json"
        if themeMode == .night {
            path = "night.json"
        }
        guard let filePath = Bundle.main.path(forResource: path, ofType: nil) else {return}
        let fileUrl = URL(fileURLWithPath: filePath)
        guard let data = try? Data(contentsOf: fileUrl) else { return  }
        guard let dict = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) else {dPrint("主题文件配置错误"); return }
        currentTheme = dict as! [String:Any]
    }
    
    func changeTheme(theme: ThemeMode) {
        if theme == themeMode {
            return
        }
        themeMode = theme
        loadThemeInfo()
        AppCacheInfo.default.theme = theme.rawValue
        NotificationCenter.default.post(name: NSNotification.Name.Theme.ThemeNeedChange, object: nil)
    }
    
    private struct ThemeKeys {
        public static let ColorsKey = "colors"
        public static let ImagesKey = "images"
    }
    
    func currentThemeMode() -> ThemeMode {
        return themeMode
    }
    
    func themeColor(_ forKey: String) -> UIColor {
        guard let obj = currentTheme[ThemeKeys.ColorsKey] else { return UIColor.clear }
        guard let colorDict = obj as? [String:String] else { return UIColor.clear }
        guard let hex = colorDict[forKey] else {dPrint("没有查找到"+forKey+"颜色的配置"); return UIColor.clear}
        return UIColor(hexStr: hex)
    }
    
    func themeImage(_ forKey: String) -> UIImage? {
        guard let obj = currentTheme[ThemeKeys.ImagesKey] else { return nil }
        guard let imageDict = obj as? [String:String] else { return nil }
        guard let imageName = imageDict[forKey] else {dPrint("没有查找到"+forKey+"图片的配置"); return nil}
        return UIImage(named: imageName)
    }
    ///主题配置文件中所有颜色的key值
    struct ThemeColorKeys {
        ///一级标题颜色
        public static let text1 = "color_text1"
        ///二级标题及辅助性标题颜色
        public static let text2 = "color_text2"
        ///输入及次要字体颜色
        public static let text3 = "color_text3"
        public static let separator = "color_separator"
        public static let border = "color_border"
        public static let btnText = "color_btnText"
        ///背景色和导航栏颜色
        public static let background = "color_background"
        ///取反背景色
        public static let invertBackground = "color_invertBackground"
        ///间隙颜色
        public static let margin = "color_margin"
        ///资金明细等特殊对应背景色
        public static let background2 = "color_background2"
        ///列表间隙的线颜色
        public static let tabLineColor = "color_tabLineColor"
        ///列表选择背景颜色
        public static let selectTabColor = "color_selectTabColor"
        ///列表间隙颜色
        public static let background3 = "color_background3"
        ///k线更多弹框背景色
        public static let background4 = "color_background4"
        ///边框颜色
        public static let borderColor = "color_borderColor"
        public static let tabbarTitle = "color_tabbarTitle"
        public static let tabbarBackground  = "color_tabbarBackground"
        
    }
    
    ///主题配置文件中所有图片的key值
    struct ThemeImageKeys {
        public static let test = "image_test"
        public static let coinRetract = "image_coinRetractRight"
        public static let coinRetractRight = "image_coinRetract"
        public static let qrCode = "image_qrCode"
        public static let assetAddress = "image_Address"
        public static let coinAll = "image_coinAll"
        public static let screen = "image_screen"
        public static let appeal = "image_appeal_back"
        public static let tabHomeNormal = "image_tab_home_normal"
        public static let tabHomeSelected = "image_tab_home_selected"
        public static let tabExchangeNormal = "image_tab_exchange_normal"
        public static let tabExchangeSelected = "image_tab_exchange_selected"
        public static let tabTradeNormal = "image_tab_trade_normal"
        public static let tabTradeSelected = "image_tab_trade_selected"
        public static let tabLeveragedNoraml = "image_tab_leveraged_normal"
        public static let tabLeveragedSelected = "image_tab_leveraged_selected"
        public static let tabMineNormal = "image_tab_mine_normal"
        public static let tabMineSelected = "image_tab_mine_selected"
        public static let mineSetting = "image_mine_setting"
        public static let tradeCopy = "image_trade_copy"
        public static let tradeTip = "image_trade_tip"
        public static let nomessage = "image_nomessage"
    }
    
}
